package com.itcodai.utils.stream;

import com.alibaba.fastjson.JSONObject;
import com.itcodai.entity.TransInfor;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author c-fanqj
 * @date 2023/9/9 0009
 * @apiNote
 */
public class StreamGroupExercise {

    public static void main(String[] args) {
        List<TransInfor> transInforList = new ArrayList<>();
        /*
        transInforList.add(new TransInfor("101", "2023-09-09 12:03:00", new BigDecimal("200")));
        transInforList.add(new TransInfor("202", "2023-09-09 12:03:00", new BigDecimal("200")));
        transInforList.add(new TransInfor("101", "2023-09-09 12:04:00", new BigDecimal("200")));
        transInforList.add(new TransInfor("202", "2023-09-09 12:04:00", new BigDecimal("200")));
        transInforList.add(new TransInfor("101", "2023-09-09 12:05:00", new BigDecimal("200")));
        transInforList.add(new TransInfor("202", "2023-09-09 12:05:00", new BigDecimal("200")));
        */
        Collections.addAll(transInforList,
                new TransInfor("101", "2023-09-09 12:03:00", new BigDecimal("200")),
                new TransInfor("202", "2023-09-09 12:03:00", new BigDecimal("200")),
                new TransInfor("101", "2023-09-09 12:04:00", new BigDecimal("200")),
                new TransInfor("202", "2023-09-09 12:04:00", new BigDecimal("200")),
                new TransInfor("101", "2023-09-09 12:05:00", new BigDecimal("200")),
                new TransInfor("202", "2023-09-09 12:05:00", new BigDecimal("200"))
                );
        //method1(transInforList);
        method2(transInforList);
    }

    // 按照时间分组
    public static void method1(List<TransInfor> transInforList) {
        System.out.println("分组之前的数据=="+transInforList);
        //transInforList.forEach(System.out::println);
        Map<String, List<TransInfor>> listMap = transInforList.stream()
                .collect(Collectors.groupingBy(TransInfor::getLastTime));
        //System.out.println("map==" + listMap);
        Set<Map.Entry<String, List<TransInfor>>> entries = listMap.entrySet();
        List<JSONObject> resultList = new ArrayList<>();
        JSONObject object;
        for (Map.Entry<String, List<TransInfor>> entry : entries) {
            object = new JSONObject();
            object.put("time", entry.getKey());
            object.put("data", entry.getValue());
            resultList.add(object);
        }
        //System.out.println("排序前 resultList==" + resultList);
        List<JSONObject> resultList2 = resultList.stream()
                .sorted((e1,e2)->compareTime(e1.getString("time"),e2.getString("time")))
                .collect(Collectors.toList());
        //System.out.println("排序后 resultList2==" + resultList2);
    }

    public static int compareTime(String time1, String time2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        long timeLong1 = 0;
        long timeLong2 = 0;
        try {
            timeLong1 = sdf.parse(time1).getTime();
            timeLong2 = sdf.parse(time2).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (int) (timeLong1 - timeLong2);
    }

    // 按照交易分组
    public static void method2(List<TransInfor> transInforList) {
        //System.out.println("分组之前的数据=="+transInforList);
        //transInforList.forEach(System.out::println);
        Map<String, List<TransInfor>> listMap = transInforList.stream()
                .collect(Collectors.groupingBy(TransInfor::getTransName));
        //System.out.println("map==" + listMap);
        Set<Map.Entry<String, List<TransInfor>>> entries = listMap.entrySet();
        List<JSONObject> resultList = new ArrayList<>();
        JSONObject object;
        for (Map.Entry<String, List<TransInfor>> entry : entries) {
            object = new JSONObject();
            object.put("trans", entry.getKey());
            object.put("data", entry.getValue());
            object.put("data", entry.getValue().stream()
                    .sorted((e1,e2)->compareTime(e1.getLastTime(),e2.getLastTime()))
                    .collect(Collectors.toList())
            );
            resultList.add(object);
        }
        //System.out.println("排序后 resultList2==" + resultList);
    }

}
